<?php
namespace AdminApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class AdminSettingsApiController extends AbstractRestfulController
{
    public function getList()
    {
		header('Access-Control-Allow-Origin: *');
		$AdminSettingsTable=$this->getServiceLocator()->get('Models\Model\AdminSettingsFactory');
		$getAllSettings = $AdminSettingsTable->getAllSettings()->toArray();
		//echo $getAllSettings[0]['numberof_success_per_day'];exit;
		if(count($getAllSettings)!=0){
				return new JsonModel(array(
					'value'				=>	1,
					'success_per_day'	=> $getAllSettings[0]['numberof_success_per_day'],
					'data'				=> json_encode($getAllSettings),
				));
			}else{
				return new JsonModel(array(
					'value'			=>	0,
				));
			}
    }
    public function get($saveAdminSettings)
    {	
	
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');
		$AdminSettingsTable=$this->getServiceLocator()->get('Models\Model\AdminSettingsFactory');
		$allSettings = $AdminSettingsTable->checkAdmindata($data);
		if($allSettings!=0){
			return new JsonModel(array(
				'value'			=> 1,
				'result'		=> json_encode($allSettings),
			));
		}else{
			return new JsonModel(array(
				'value'			=> 0,
			));
		}
    }
    public function update($id,$data)
    {
		header('Access-Control-Allow-Origin: *');
		$AdminSettingsTable=$this->getServiceLocator()->get('Models\Model\AdminSettingsFactory');
		$logoimage="";
		$regimage="";
		if($data['win_logo']!=""){
			$content=file_get_contents($data['win_logo']);
			$image='winlogo';
			$fp = fopen("public/uploads/adminimages/".$image.".jpg", "w");
			fwrite($fp, $content);
			fclose($fp);
			$logoimage=$image.".jpg";
		}
		if($data['reg_image']!=""){
			$content=file_get_contents($data['reg_image']);
			$imagee='reg';
			$fp = fopen("public/uploads/adminimages/".$imagee.".jpg", "w");
			fwrite($fp, $content);
			fclose($fp);
			$regimage=$imagee.".jpg";
		}
		$updateSettings = $AdminSettingsTable->updateAdminSettings($id,$data,$regimage,$logoimage);
		return new JsonModel(array(
			'value'			=> 1,
		));
	}		
    public function delete($id)
    {
       
    }
}